package com.yy.myp.provider;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.location.provider.ProviderProperties;
import android.os.Build;
import android.os.SystemClock;
import android.util.Log;

import com.baidu.mapapi.model.LatLng;
import com.yy.myp.entity.Point;
import com.yy.myp.utils.GPSUtil;

import java.util.List;

public class MockLocationProvider {
//    private String providerName;
    private Context ctx;
    private Boolean mockRun;
    private List<String> providerNames;

    private Point pPoint;

    public MockLocationProvider(List<String> providerNames, Context ctx) {
        this.ctx = ctx;
        this.mockRun = false;
        this.providerNames = providerNames;
        if (null == providerNames || providerNames.isEmpty()) {
            throw new RuntimeException("服务提供者不可为空");
        }

        LocationManager lm = (LocationManager) ctx.getSystemService(
                Context.LOCATION_SERVICE);
        for (String providerName : providerNames) {
            lm.addTestProvider(providerName, false, false, false, false, true,
                    true, true, ProviderProperties.POWER_USAGE_HIGH, ProviderProperties.ACCURACY_FINE);
            lm.setTestProviderEnabled(providerName, true);
        }
        new Thread( () -> {
            while (true) {
                try {
                    Thread.sleep(1000);

                    if (!mockRun) {
                        continue;
                    }
                    try {
                        for (String providerName : providerNames) {
                            Location mockLocation = new Location(providerName);
                            mockLocation.setLatitude(pPoint.getLatitude());
                            mockLocation.setLongitude(pPoint.getLongitude());
                            mockLocation.setAltitude(0);
                            mockLocation.setAccuracy(ProviderProperties.ACCURACY_FINE);
                            mockLocation.setTime(System.currentTimeMillis());
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                                mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
                            }
                            lm.setTestProviderLocation(providerName, mockLocation);
//                            Toast.makeText(ctx, "Working", Toast.LENGTH_SHORT).show();
                            Log.v("TAG", String.format("模拟定位<%s, %s>", pPoint.getLatitude(), pPoint.getLongitude()));
                        }
                    } catch (Exception e) {
                        this.stopMock();
                        Log.v("TAG", "模拟定位失败");
                        Log.v("TAG", e.toString());
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    public void pushLocation(LatLng mockLatLng) {
//        this.mockRun = true;

        double[] points = GPSUtil.bd09_To_gps84(mockLatLng.latitude, mockLatLng.longitude);
        this.pPoint = new Point(points[0], points[1]);
    }

    public boolean startMock () {
        if (null != this.pPoint) {
            this.mockRun = true;
            return true;
        }
        return false;
    }

    public boolean isRunning () {
        return this.mockRun;
    }

    public void stopMock () {
        this.mockRun = false;
    }

    public void shutdown() {
        this.stopMock();
        for (String providerName : providerNames) {
            LocationManager lm = (LocationManager) ctx.getSystemService(
                    Context.LOCATION_SERVICE);
            lm.removeTestProvider(providerName);
        }
    }
}
